
"use strict";
// store user action state 
// set, get user state 
function CExMgr () {
	
	this.user_states = [

	];
	
	this.servers = {
		 'hipo': {name:'hipo', serverurl: 'http://k2.emrosswar.com/'} ,
		 'Ania':	{name:'Ania', serverurl: 'http://s103.emrosswar.com/'} ,
	};
	
	
	this.ex_us = [
			[ 'wipe9out', 'hipo', 'greateload', 'e7e8348982342f898b87895334345228985'  , '1212'], 
			[ 'dominate', 'hipo', 'greateload', 'e7e83532356ia2342354545334345228985' , '1212'], 
			[ 'extinctor', 'hipo', 'greateload', 'e7e83545756876978677856745645228985' , '1212'] ,
	];
			

	

	
	// this.initialize();
};


CExMgr.prototype = {
	objName: 'CExMgr',
	
	
	initialize: function() {
	},
	
	makeUserDB: function() {
		var self = this;
	},
	
	userCnt: function() {
		return this.ex_us.length;
	},
	
	getUserStates: function(i) {
		return this.ex_us[i];
	},
	
	
};


// get user state from user table 
//  if need to login : logn 
//   start step action 

// action ste p:    set query, set cstlmgr info  with prev state . 
//  check which step : substep 
// do cur step  
function CExtinctorMgr() {
	this.objName = "CExtinctorMgr: ";
	this.initialize();
};


CExtinctorMgr.prototype = {
	
	// get user state : ?? 
	initialize: function() {
		this.u_idx = -1;
	},
	
		// entry ::: do...
	// get user state 
	doAction: function() {
		var self = this;
		debug.log(this,  ' doAction ========== ');

		var dfd =  $.Deferred();
		
		// get next user
		
		if ( ++this.u_idx  >= em.spusermgr.userCnt() ) {
			debug.log( this, ' Wrap all Users ', this.u_idx);
			this.u_idx = 0;
		}
		
		// checkUserState
		// get state
		var new_u_s = em.exmgr.getUserStates( this.u_idx ); 
		debug.log(this,  '   * nane :  ', new_u_s.name, new_u_s , ' idx:', this.u_idx );
		
		
 
		if ( ! new_u_s.started ) {
			
				// create user view 
			new_u_s.view = new CExtinctView( new_u_s[0] );

			self.switchUserCtx( new_u_s );
		
			// make new sp action obj 
			new_u_s.exa = new CExtinctAction();
			
			// if (self.cb_userchanged  )
					// em.cbs.userchanged.remove( self.cb_userchanged );
								// cb when cstl move done 
			em.cbs.userchanged.add( self.cb_userchanged = function( ret ){
					// fail 
					if ( ret == -1 ) {	
						// go next user
						 dfd.reject( 1 );
						 new_u_s.exist = false;
						 return;
					}
					debug.log(self, 'this', new_u_s.name, ' :: cb fn:  before   ' ,  em.cbs.userchanged,  self.cb_userchanged); 
					em.cbs.userchanged.remove( self.cb_userchanged );
					debug.log(self, 'this', new_u_s.name, ' :: cb fn:   after ' , em.cbs.userchanged,   self.cb_userchanged); 
					debug.log(self, 'this', new_u_s.name, 'cb_cstlmove in start SP GM  '); 
					
					new_u_s.started = true;
					
					// store cur states 
					em.exmgr.setUserStates (  em.cstlmgr.getAllStates(new_u_s) );
					
					
					self.waveCstls();
					// to start next right after..  
					// dfd.reject( 1 );
				
				}  );
				
			// do login :    async : go next 
			em.accmgr.startGM( new_u_s.name);
			return dfd.promise();
		}

		this.switchUserCtx( new_u_s );
		
		
			//  lv > 30 : skip  :  precondition : setCurUser :: <- switch user .. fn 
			// after switch User ctx
		// disable lv check to donate !!!   
/*
		var userinfo = CDB.getCurUserInfo();
		if ( userinfo.grade >= 30 ){
					// noty  view 
					new_u_s.view.setFinishedState(true);
					
					dfd.resolve();
					return dfd.promise();
		}*/

		
		// start sp action 
		// this.startSPAction( new_u_s , dfd );
		new_u_s.sps.startSPAction(  dfd );
		
	// afte done.. call this.. 		
			// store cur states 
/*
				em.spusermgr.setUserStates ( this.u_idx, em.cstlmgr.getAllStates() );
		
		// ============		
				// switch  cur user.
	  	// em.spproxy.changeCurUser( new_u_states );
	  	
	  	// update view 
		// em.sp30view.updateUser( new_u_s );
		
		// start view refresh 
		em.sp30view.startRefresh();		*/

		
		return dfd.promise();
	},
	
	switchUserCtx: function( u_s ) {
				// connect view 
			// sp30 mode . info -> sp30 view. 
			em.infoview = u_s.view;
			em.cstlmgr.setView(u_s.view);
			u_s.view.current();
			
				// prepare context for cur user
				// set cur user : set cur uset to DB,  & get session key  & set Query : device,  server    
			em.accmgr.setCurUser(u_s.name);
				
			// set cur user 's cstlmgr info 
			em.cstlmgr.setNewUserInfo( u_s.castles );		
	},
	
	// stop seletec user sp action 
	stopUserAction: function( username) {
			var u_s = em.spusermgr.getUserStatesByName( username ); 
			debug.log(this,  '   * nane :  ', username , u_s.name , u_s );
		
			u_s.sps.stop (  u_s );
	},
	
	resumeUserAction: function( username) {
			var u_s = em.spusermgr.getUserStatesByName( username ); 
			debug.log(this,  '   * nane :  ', username , u_s.name , u_s );
		
			u_s.sps.resume (  u_s );
	},
	
	waveCstls: function( ) {
		
	},
	
	
};



$(document).ready(function() {

	// cbs 
		em.cbs= {};
		
		// for cstl move & wave 
		em.cbs.cstlmove1done = $.Callbacks( "unique stopOnFalse" );	
		em.cbs.cstlwavedone = $.Callbacks( "unique stopOnFalse" );
		
			
		// login new user : got new key 
		em.cbs.userlogin = $.Callbacks("unique stopOnFalse");
		
		// when completed select new user : start GM  -> new login : get system task api :: then fire . :  reset all prev datas  
		em.cbs.userchanged = $.Callbacks("unique stopOnFalse");
			// when start user change :  to notify free prev resource  
		em.cbs.startuserchange = $.Callbacks("unique stopOnFalse");
		
		// all hero list update
		em.cbs.heroupdate = $.Callbacks("unique stopOnFalse");
		// hnting avaliable h list update 
		em.cbs.warableHpdate = $.Callbacks("unique stopOnFalse");
		
		// hnt  : 1 da hnt 1 time end cb  for cals end time : decide next ctsl
		em.cbs.hntdone = $.Callbacks("unique stopOnFalse");
		// 1 cstl att is done !!
		em.cbs.autoCstlAttDone = $.Callbacks("unique stopOnFalse");  
		// update cstl 's sols '
		em.cbs.updatesols = $.Callbacks("unique stopOnFalse");
		
		// sols trained finished  
		em.cbs.soltraindone = $.Callbacks("unique stopOnFalse");
		
		// global contorl cb 
		em.cbs.forcestop = $.Callbacks("unique stopOnFalse");
		
		// timer for desc time info  
		em.cbs.sectimer = $.Callbacks("unique stopOnFalse");
		
		
	// global 
		// var users = CDB.users;//[ 'james', 'greate'];
		// var devices = CDB.devices;//['iphone', 'android' ] ;
		
	// em.cstlview = new CCstlView();
	// em.cstlview.initialize();

/*

	em.sp30view = new CSP30View();

	// sp30 mode . info -> sp30 view. 
	em.infoview = em.sp30view ;
	// em.infoview.initialize();
//	em.infoview.connected();	*/

		
	em.cstlmgr = new CCstlMgr();
	em.cstlmgr.cstlwave_initialize();



	em.query = new CQuery();
	em.query.initialize();

	em.util = new CUtil();
		
	em.tasks = new CTasks();
	
	em.worldDA = new CWorldDA();
	
	em.hntmgr = new CHuntMgr();
	em.hntmgr.initialize();
	
	em.controller = new CController();
	
	em.scaning = new CScaning();
	em.scaning.initialize();

	em.hdrinmgr = new CHDrinMgr();
	
	
	// start monotor !!
	em.jobmonitor.registerCBs();
	em.jobmonitor.startMonitor();

	



	em.exmgr = new CExMgr();
	
	em.spactmgr = new CSpActionMgr();
	//======================= TEST code here ========================
	// set login defualt user 'Greateload' for test 
	// em.controller.login('Greateload');
		

	


	/*
		 set nav with ref names 
		 
	*/

   		// start sp job  when load completed...  
   		
   			$('.container1').click(function(e) {
				em.curOp ^= em.Operation.EXGROWING;
				
				//$("#id_btn_buytrade").toggleClass('control_modeon');
				
				if ( em.curOp & em.Operation.EXGROWING) {
					// debug.log(this,  ' EXGROWING clicked ' , em.curOp.toString(16) );
					
					debug.log( this, '::info  : ');
					
					em.jobmonitor.addJob( {name: 'EXGROWING', fn: em.jobmonitor.doExgrowing, param: undefined });
					em.jobmonitor.doMonitor();
					return;
				}	else {
					debug.log(this,  ' Mode release  ' , em.curOp.toString(16));
				}	   		
			});
});
  





